package jam.network.message;

import jam.network.NetworkController;
import jam.model.ClientModel;
import java.util.Scanner;

/**
 * A class that parses messages sent from server to client
 */
public class MessageParser {

	/**
	 * Calls the correct process method based on the message
	 *
	 * @param msg the string sent over the network
	 */
	public static void parseMessage(String msg, ClientModel clientModel) {
		Scanner s = new Scanner(msg);
		s.useDelimiter(":");

		//Attempt to scan the string
		String keyword;
		int transID;
		try {
			keyword = s.next();
			transID = s.nextInt();
		}
		catch (Exception e) { 
			System.out.println(e.toString());
			return;
		}

		//call the correct process
	    if (keyword.equalsIgnoreCase("CA_REGISTER"))
			ResponseRegister.process(msg);
		else if (keyword.equalsIgnoreCase("CA_LOGIN")) 
			ResponseLogin.process(msg, clientModel);
		else if (keyword.equalsIgnoreCase("CA_JOIN"))
			ResponseJoinChannel.process(msg, clientModel);
		else if (keyword.equalsIgnoreCase("CA_LEAVE"))
			ResponseLeaveChannel.process(msg, clientModel);
		else if (keyword.equalsIgnoreCase("CA_ADD"))
			ResponseAddContact.process(msg);
		else if (keyword.equalsIgnoreCase("CA_REMOVE"))
			ResponseRemoveContact.process(msg, clientModel);
		else if (keyword.equalsIgnoreCase("CA_MSG"))
			ResponseSendMessage.process(msg);
		else if (keyword.equalsIgnoreCase("CA_INVITE"))
			ResponseInvite.process(msg);
		else if (keyword.equalsIgnoreCase("CA_LOCK"))
			ResponseLock.process(msg);
		else if (keyword.equalsIgnoreCase("CA_PASSWORD"))
			ResponsePassword.process(msg);
		else if (keyword.equalsIgnoreCase("CR_USERINFO"))
			ResponseUserInfo.process(msg, clientModel);
		else if (keyword.equalsIgnoreCase("CR_USERLIST"))
			ResponseUserList.process(msg);
		else if (keyword.equalsIgnoreCase("CR_CONTACTLIST"))
			ResponseContactList.process(msg);
		else if (keyword.equalsIgnoreCase("CR_CHANNELLIST"))
			ResponseChannelList.process(msg);
		else if (keyword.equalsIgnoreCase("SA_JOIN"))
			ServerActionJoin.process(msg);
		else if (keyword.equalsIgnoreCase("SA_LEAVE"))
			ServerActionLeave.process(msg);
		else if (keyword.equalsIgnoreCase("SA_INVITE"))
			ServerActionInvite.process(msg);
		else if (keyword.equalsIgnoreCase("SA_MSG"))
			ServerActionMessage.process(msg);
		else if (keyword.equalsIgnoreCase("SA_PING"))
			ServerActionPing.process(msg);
	}


}